<?php
// Display a table of events that the swimmer took part in.
function showEventHistory () {
	echo "<div class='featured-post'>
            <div class='post-title'>
                <h2>Your Event History</h2>
			</div>
			<div class='post-entry'>";
			
	// Get the page number so we know which page to display.
	$pageNum = $_GET['pagenum'];
	if (!(isset($pageNum))) { 
		$pageNum = 1; 
	}
			
	$myID = $_SESSION['ls_userid'];
	// Get all records of the swimmer from swimPerformance table.
	// Also get the events name, type, length, and date from ls_events table.
	$query = "SELECT `swimPerformance`.`swimmerID` AS swimmerID,
				`swimPerformance`.`eventID` AS swim_eventID,
				`ls_events`.`eventID` AS event_eventID
				FROM `swimPerformance`, `ls_events`
				WHERE swimmerID = '$myID'
				AND `swimPerformance`.`eventID` = `ls_events`.`eventID`";
	$list = mysql_query($query) or die("Error getting event history 1: " . mysql_error());			
	// Get the total number of comments
	$totalRows = mysql_num_rows($list);
	
	// Show comments only if there are comments for the current user.
	if($totalRows == 0) {
		echo "<p>You have no event history.</p>";
	}
	else {
		// Number of results to show per page.
		$rowLimit = 20;
		
		// Number for last page.
		$last = ceil($totalRows / $rowLimit);
		
		// Make sure page number is in range.
		if($pageNum < 1) {
			$pageNum = 1;
		}
		else if ($pageNum > $last) {
			$pageNum = $last;
		}
		
		// Set the limit range for the sql query. Use $max to get the subset of
		// records to display for the chosen page based on pagination.
		if($pageNum > 0) {
			$max = 'LIMIT ' .($pageNum - 1) * $rowLimit . ',' . $rowLimit;
		}
		
		// Get all records of the swimmer from swimPerformance table.
		// Also get the events name, type, length, and date from ls_events table.
		$query = "SELECT `swimPerformance`.`swimmerID` AS swimmerID,
					`swimPerformance`.`eventID` AS swim_eventID,
					`ls_events`.`eventID` AS event_eventID,
					`ls_events`.`name` AS eventName,
					`ls_events`.`eventType` AS eventType,
					`ls_events`.`eventLength` AS length,
					`ls_events`.`eventDate` AS date
					FROM `swimPerformance`, `ls_events`
					WHERE swimmerID = '$myID'
					AND `swimPerformance`.`eventID` = `ls_events`.`eventID`
					ORDER BY date DESC $max";
		
		$list = mysql_query($query) or die("Could not get event history 2: " . mysql_error());

		// Print out the list of event records in an html table.
		echo "<table id='blueTable'><tr><th>Event Name</th><th>Event Type</th><th>Length</th><th>Date</th><th>View Results</th></tr>";
		$i = 0;
		while($rows = mysql_fetch_assoc($list)) {
			// Use the $style variable to alternate the css class.
			$style = (($i % 2) == 0)? 'even' : 'odd';
			echo "<tr class='$style'><td>" . $rows['eventName'] . "</td><td>" . $rows['eventType'] . "</td><td>" . $rows['length'] . 
				"</td><td>" . $rows['date'] . "</td><td><a href='" . substr($_SERVER['PHP_SELF'], 0, lastIndexOf($_SERVER['PHP_SELF'], '/')) . "/eventResults.php?eventID=" . $rows['swim_eventID'] . "'>View</a></td></tr>";
			$i++;
		}
		echo "</table>";
		
		// Do pagination if there are more than 1 pages of results to show.
		if($last > 1) {
			paginate($pageNum, $last);
		}
	}
	echo "</div>\n</div>";
}

// Get the last index of a character in a string.
function lastIndexOf($string,$item){  
    $index=strpos(strrev($string),strrev($item));  
    if ($index){  
        $index=strlen($string)-strlen($item)-$index;  
        return $index;  
    }  
    else { 
        return -1;
	}		
}

// Display a table of the event results by getting the performance
// of all swimmers of the event to be displayed..
function showEventResults() {
	echo "<div class='featured-post'>
            <div class='post-title'>
                <h2>Your Event Results</h2>
			</div>
			<div class='post-entry'>";
			
	// Get the page number and eventID so we know which page  and which event to display.
	$eventID = $_GET['eventID'];
	if (!(isset($eventID))) { 
		echo "<p>No event chosen to be displayed.</p>";
		exit;
	}
	$pageNum = $_GET['pagenum'];
	if (!(isset($pageNum))) { 
		$pageNum = 1; 
	}	
			
	$myID = $_SESSION['ls_userid'];
	// Get all records of swimmers from swimPerformance table.
	// Also get the events name, type, length, and date from ls_events table.
	$query = "SELECT * FROM `swimPerformance` WHERE `eventID` = '$eventID'";
	$list = mysql_query($query) or die("Error getting event history 1: " . mysql_error());			
	// Get the total number of comments
	$totalRows = mysql_num_rows($list);
	
	// Show comments only if there are comments for the current user.
	if($totalRows == 0) {
		echo "<p>You have no event history.</p>\n</div>";
	}
	else {
		// Number of results to show per page.
		$rowLimit = 10;
		
		// Number for last page.
		$last = ceil($totalRows / $rowLimit);
		
		// Make sure page number is in range.
		if($pageNum < 1) {
			$pageNum = 1;
		}
		else if ($pageNum > $last) {
			$pageNum = $last;
		}
		
		// Set the limit range for the sql query.
		if($pageNum > 0) {
			$max = 'LIMIT ' .($pageNum - 1) * $rowLimit . ',' . $rowLimit;
		}
		
		// Get all records of the swimmer from swimPerformance table.
		// Also get the events name, type, length, and date from ls_events table.
		$query = "SELECT `swimPerformance`.*, `accounts`.`name` AS name,
					`ls_events`.`name` AS eName, `ls_events`.`eventID`, 
					`ls_events`.`eventType` AS type, `ls_events`.`eventLength` AS length
					FROM `swimPerformance`, `accounts`, `ls_events`
					WHERE `swimPerformance`.`eventID` = '$eventID'
					AND `swimPerformance`.`swimmerID` = `accounts`.`userID`
					AND `swimPerformance`.`eventID` = `ls_events`.`eventID`
					ORDER BY `swimPerformance`.`place` ASC $max";
		
		$list = mysql_query($query) or die("Could not get event history 2: " . mysql_error());

		// Print out the list of event records in an html table.
		$rows = mysql_fetch_assoc($list);
		echo "<br/><h3>" . $rows["eName"] . " " . $rows["type"] . " " . $rows["length"] . "</h3>" .
			"<table id='blueTable'><tr><th>Name</th><th>Time</th><th>Place</th></tr>";
		mysql_data_seek($list, 0);
		$i = 0;
		while($rows = mysql_fetch_assoc($list)) {
			// Use the $style variable to alternate the css class.
			$style = (($i % 2) == 0)? 'even' : 'odd';
			echo "<tr class='$style'><td>" . $rows['name'] . "</td><td>" . $rows['time'] . "</td><td>" . $rows['place'] . "</td></tr>";
			$i++;
		}
		echo "</table>";
		
		// Do pagination if there are more than 1 pages of results to show.
		if($last > 1) {
			paginate($pageNum, $last);
		}
	}
	echo "</div>\n</div>";
}
?>